package com.ybc.chinese.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ybc.chinese.R;
import com.ybc.chinese.adapter.IdiomAdapter;
import com.ybc.chinese.bean.Idiom;
import com.ybc.chinese.utils.CacheUtil;
import com.ybc.chinese.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * <p>成语<p/>
 * Created by Yubch on 17/2/16.
 */
public class IdiomFragment extends BaseFragment /*implements PullToRefreshBase.OnRefreshListener2<ListView>*/ {
    @Bind(R.id.lv_idiom)
    PullToRefreshListView lvIdiom;
    private Context mCtx;
    public static final String INDEX_Idiom = "index/idiom";
    //private int mOffset = 0;
    private IdiomAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mCtx = getActivity();
        View view = inflater.from(mCtx).inflate(R.layout.fragment_idiom, container, false);
        ButterKnife.bind(this, view);
        initView();
        List<Idiom> idioms = getCache();
        if(!CommonUtil.isEmpty(idioms)) {
            mAdapter.setData(idioms, false);
        } else {
            showRunningDialog();
            reqeust();
        }
        return view;
    }

    private void initView() {
        mAdapter = new IdiomAdapter(mCtx);
        lvIdiom.setAdapter(mAdapter);
//        lvIdiom.setMode(PullToRefreshBase.Mode.BOTH);
//        lvIdiom.setOnRefreshListener(this);
    }

    private List<Idiom> getCache() {
        String jsonIdiom = CacheUtil.getCache(mCtx, INDEX_Idiom);
        List<Idiom> idioms = new ArrayList<>();
        if(!TextUtils.isEmpty(jsonIdiom)) {
            idioms = new Gson().fromJson(jsonIdiom, new TypeToken<List<Idiom>>(){}.getType());
        }
        return idioms;
    }

    private void reqeust() {
        if (!CommonUtil.isNetworkConnected(mCtx)) {
            dismissRunningDialog();
            CommonUtil.showToast(mCtx, R.string.no_net);
            lvIdiom.onRefreshComplete();
            return;
        }
        BmobQuery<Idiom> query = new BmobQuery<>();
//        query.setLimit(Contants.LIMIT);
//        query.setSkip(mOffset);
        //按照时间降序
        query.order("-myOrder");
        //执行查询，第一个参数为上下文，第二个参数为查找的回调
        query.findObjects(new FindListener<Idiom>() {

            @Override
            public void done(List<Idiom> list, BmobException e) {
                if (e == null) {
                    if(!CommonUtil.isEmpty(list)) {
                        //if(mOffset == 0) {
                            mAdapter.setData(list, false);
                            CacheUtil.saveCache(mCtx, INDEX_Idiom, new Gson().toJson(list));
//                        } else {
//                            mAdapter.setData(list, true);
//                        }
//                        mOffset = mOffset + list.size();
                    }
                } else {
                    CommonUtil.showToast(mCtx, "数据请求失败");
                }
                dismissRunningDialog();
                lvIdiom.onRefreshComplete();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /*@Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        mOffset = 0;
        reqeust();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        reqeust();
    }*/
}
